package com.qst.rongxiaoserver.controller;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.qst.rongxiaoserver.common.Result;

import com.qst.rongxiaoserver.model.UserModel;

import com.qst.rongxiaoserver.model.dto.UserDto;

import com.qst.rongxiaoserver.service.IUserService;

import io.swagger.v3.oas.annotations.Operation;

import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.*;



import java.util.List;



@RestController

@RequestMapping("/api/user")

@Tag(name = "用户管理", description = "用户管理相关接口(服务层实现)")

public class UserController {
    @Autowired
    IUserService userService;

    // 添加用户

    @PostMapping("/add")

    @Operation(summary = "添加用户", description = "添加用户(服务层实现)")

    public Result<UserModel> addUser(@Valid @RequestBody UserModel userModel, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return Result.fail("参数校验失败: " + bindingResult.getFieldError().getDefaultMessage());

        }

       boolean success =   userService.save(userModel);

       return success? Result.success(userModel,"添加成功"):Result.fail("添加失败");

    }
    // 根据用户名修改个人信息

    @PostMapping("/update")

    @Operation(summary = "编辑用户信息", description = "编辑用户(服务层实现)")

    public Result<UserModel> updateUser(@Valid @RequestBody UserModel userModel, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return Result.fail("参数校验失败: " + bindingResult.getFieldError().getDefaultMessage());

        }

        boolean success =   userService.updateUser(userModel);

        return success? Result.success(userModel,"编辑成功"):Result.fail("编辑失败");

    }

    // 更新密码

    @PostMapping("/update/password")

    @Operation(summary = "更新密码", description = "更新密码(服务层实现)")

    public Result updatePassword(@Valid @RequestBody UserDto userDto, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {

            return Result.fail("参数校验失败: " + bindingResult.getFieldError().getDefaultMessage());

        }

        boolean success =   userService.updatePassword(userDto);

        return success? Result.success(userDto,"编辑成功"):Result.fail("编辑失败");

    }
    // 查询用户
    @GetMapping("/list")
    @Operation(summary = "用户列表", description = "用户列表(服务层实现)")
    public Result getUsers(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           @RequestParam(value = "phone", required = false) String phone,
                           @RequestParam(value = "nickName", required = false)  String nickName) {

       Page<UserModel> userList =  userService.getUserList(pageNum, pageSize, phone, nickName);
        return userList!=null ? Result.success(userList,"编辑成功"):Result.fail("编辑失败");
    }

    // 删除用户
    @DeleteMapping("/delete")
    @Operation(summary = "根据用户名删除用户", description = "根据用户名删除用户(服务层实现)")
    public Result deleteUser(@RequestParam String userName) {
        boolean success =   userService.removeById(userName);
        return success? Result.success("删除成功"):Result.fail("删除失败");
    }

  // 获取用户详情
    @GetMapping("/detail")
    @Operation(summary = "根据用户名获取用户详情", description = "根据用户名获取用户详情(服务层实现)")
    public Result<UserModel> detailUser(@RequestParam String userName) {
        UserModel userModel =  userService.getById(userName);
        return userModel!=null? Result.success(userModel,"编辑成功"):Result.fail("编辑失败");
    }
}
